package com.fjec.market.controller;

import com.fjec.common.annotation.Json;
import com.fjec.common.protocol.JsonResult;
import com.fjec.market.entity.BizServOrder;
import com.fjec.market.schedule.handler.RequestServDivideHandler;
import com.fjec.market.service.IBizServOrderService;
import com.fjec.market.view.ServOrderItem;
import org.omg.CORBA.OBJECT_NOT_EXIST;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 044.服务订单表 前端控制器
 * </p>
 *
 * @author admin
 * @since 2020-09-21
 */
@RestController
@RequestMapping("")
public class BizServOrderController {

    @Resource
    private IBizServOrderService service;

    @PostMapping("/serv/order/save")
    public Long saveServOrder(@RequestParam Integer totalPrice, @RequestParam Integer orderType,
                              @RequestBody List<ServOrderItem> productList, @RequestParam String subOpenId, @RequestParam Long userId) {
        Long orderId = service.saveOrder(totalPrice, orderType, productList, subOpenId, userId);

        if (orderId != null && orderId != 0)
            return orderId;
        else
            return -1L;
    }

    /**
     * activity service call this interface after purchasing gifts
     * @param orderId
     * @return
     */
    @PostMapping("/serv/order/set/status2")
    public int setServOrderStatus2(@RequestParam("orderId") Long orderId) {
        int code = service.setServOrderStatus2(orderId);
        return code;
    }


    /**
     * 支付保证金
     * @param
     * @return
     */
    @PostMapping("/y/shop/deposit/pay")
    public JsonResult payDeposit() {
        Long orderId = service.payDeposit();
        if (orderId == null)
            return JsonResult.FAILURE;
        Map<String, Object> result = new HashMap<>();
        result.put("orderId", orderId);
        return JsonResult.success().data(result).build();
    }

    /**
     * 商户尝试给自己支付1块钱
     * @return
     */
    @PostMapping("/y/shop/self/pay")
    public JsonResult selfPay() {
        Long orderId = service.selfPay();
        if (orderId == null)
            return JsonResult.FAILURE;
        Map<String, Object> result = new HashMap<>();
        result.put("orderId", orderId);
        return JsonResult.success().data(result).build();
    }

    /**
     * paid服务在得到支付验证预支付信息后调用，设置进件结果为完全成功6
     * @param orderId
     * @return
     */
    @PostMapping("/ys/reg/self/pay/success")
    public int selfPaySuccess(Long orderId) {
        int result = service.selfPaySuccess(orderId);
        return result;
    }


    @Autowired
    private RequestServDivideHandler requestServDivideHandler;

    @GetMapping("/serv/order/divide/test")
    public String test() {
        return requestServDivideHandler.handle(10, 10);
    }
}
